'use strict';

    angular.module('app')
        .controller('userSupplyDetailController', ['$scope', '$rootScope', '$state', '$timeout', 
                                             function($scope, $rootScope, $state, $timeout) {
        var title = "流量主详情";
        var id = $state.params.id;
        
        activate(id);
        
        platfromInfo(id);
        
        $scope.title = $rootScope.title = title;
        
        $scope.loading = true;

        // 初始化页面
        $scope.businessLicenseArr = [];
        function activate(id) {
	        $scope.loading = true;
        	$.ajax({
				url : $scope.app.project_name + '/userSupply/read/detail',
				data: {'id': id}
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					console.log(result.data);
					$scope.record = result.data;
					var businessLicense = result.data.businessLicense;
					if (businessLicense != null && businessLicense != '') {
						$scope.$apply(function(){
							$scope.businessLicenseArr = businessLicense.split('|');
						})
					}else{
                            $scope.businessLicenseArr[0] =result.data.companyLogo;

					}
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
        }
        
        // 获取流量主下的平台
        function platfromInfo(id) {
            $scope.loading = true;
            $.ajax({
                url : $scope.app.project_name + '/platfrom/getPlatfromInfo',
                data: {'supplyId': id}
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.platfroms = result.data;
                } else {
                    $scope.msg = result.msg;
                }
                $scope.$apply();
            });
        }
        
        $scope.approve = function(id, state) {
        	$scope.alertMessage({
                text:"确定做此操作吗？", //内容
                btnCancel:true, //取消按钮
                btnConfirm:true //确认按钮
            }).then(function () {
            	$.ajax({
    				url : $scope.app.project_name + '/userSupply/update/approve',
    				data: {'ids': id, 'state' : state}
    			}).then(function(result) {
    		        $scope.loading = false;
    				if (result.httpCode == 200) {
                        $scope.alertModal({"content":"操作成功"});
                        $state.go('main.user.userSupply.list');
    				} else {
    					$scope.msg = result.msg;
    				}
    			});
            })
		}
        
        $scope.refuseSupply = function (id, state) {
		    $scope.userSupply = {};
			$scope.userSupply.ids = id;
	        $scope.userSupply.state = state;

	        $('#refuseUserSupply').modal('show', true);
        }
        
        $scope.saveUserSupply = function () {
            if ($scope.userSupply.approveReason == "" || $scope.userSupply.approveReason == null) {
                $scope.alertModal({"content":"请输入拒绝原因"});
                $('#refuseUserSupply').modal('hide');
                return;
            }

            $.ajax({
                url : $scope.app.project_name + '/userSupply/update/approve',
                data: $scope.userSupply
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                	$('#refuseUserSupply').modal('hide');
                    $scope.alertModal({"content":"操作成功"});
                    
                    $state.go('main.user.userSupply.list');
                } else {
                    $('#refuseUserSupply').modal('hide');
                    $scope.alertModal({"content":"操作失败"});
                }
            });
        }
        
        $scope.selected = [];
		var updateSelected = function (action, id) {
			if (action == 'add' && $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
			if (action == 'remove' && $scope.selected.indexOf(id) != -1) $scope.selected.splice($scope.selected.indexOf(id), 1);
		};

		// 全选操作
        $scope.selectAll = function ($event) {
        	var checkbox = $event.target;
        	var action = (checkbox.checked ? 'add' : 'remove');
        	
        	for (var i = 0; i < $scope.platfroms.length; i++) {
        		var contact = $scope.platfroms[i];
        		updateSelected(action, contact.id);
        	}
        };
        
        $scope.isSelected = function (id) {
        	return $scope.selected.indexOf(id) >= 0;
        };
        
        $scope.updateSelection = function ($event, id) {
        	var checkbox = $event.target;
        	var action = (checkbox.checked ? 'add' : 'remove');
			if (action == 'add' && $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
			if (action == 'remove' && $scope.selected.indexOf(id) != -1) $scope.selected.splice($scope.selected.indexOf(id), 1);
		};
		
		$scope.approvePlatfrom = function(platfromId, state) {
			$scope.alertMessage({
                text:"确定做此操作吗？", //内容
                btnCancel:true, //取消按钮
                btnConfirm:true //确认按钮
            }).then(function () {
            	var ids = [];
    			
    			if (platfromId == null || platfromId == '') {
    				ids = $scope.selected;
    			} else {
    				ids.push(platfromId);
    			}

    			var idStr = ids.join(',');
    			
    			$.ajax({
    				url : $scope.app.project_name + '/platfrom/update/approve',
    				data: {'ids': idStr, 'state' : state, 'approveFailReason' : ''},
    				traditional: true //traditional 为true阻止深度序列化
    			}).then(function(result) {
    		        $scope.loading = false;
    				if (result.httpCode == 200) {
    					$scope.selected = [];
    					platfromInfo(id);
    				} else {
    					$scope.msg = result.msg;
    				}
    			});
            })
		}
		
		$scope.refuse = function (id, state) {
		    $scope.platfromInfo = {};
		    
		    var ids = [];
			
			if (id == null || id == '') {
				ids = $scope.selected;
			} else {
				ids.push(id);
			}

			var idStr = ids.join(',');
			if (idStr == null || idStr == '') {
				$scope.alertModal({"content":"请选择需要审核的记录"});
                return;
			} else {
				$scope.platfromInfo.ids = idStr;
	            $scope.platfromInfo.state = state;

	            $('#refusePlatfrom').modal('show', true);
			}
        }
		
		$scope.saveRefuse = function () {
            if ($scope.platfromInfo.approveFailReason == "" || $scope.platfromInfo.approveFailReason == null) {
                $scope.alertModal({"content":"请选择拒绝原因"});
                $('#refusePlatfrom').modal('hide');
                return;
            }

            $.ajax({
                url : $scope.app.project_name + '/platfrom/update/approve',
                data: $scope.platfromInfo
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                	$('#refusePlatfrom').modal('hide');
                    $scope.alertModal({"content":"操作成功"});
                    
					$scope.selected = [];
					platfromInfo(id);
                } else {
                    $('#refusePlatfrom').modal('hide');
                    $scope.alertModal({"content":"操作失败"});
                }
            });
        }
		
		$scope.industryQualifications = [];
        $scope.preview = function(industryQualification){
        	if (industryQualification != null && industryQualification != '') {
				$scope.industryQualifications = industryQualification.split('|');
			}
        	$scope.firstApply = true;
        }
        
    }]);